import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'event_handlers.dart';

class Uverify {
  static final String tag = "UJVERIFY-FLUTTER";
  static MethodChannel _channel = MethodChannel('uverify');
  static final EventHandlers _eventHanders = EventHandlers();

  static Future<Map<dynamic, dynamic>> init(
      {@required String appKey,
      @required String appSecret,
      String channel = "Umeng",
      bool loggerEnable = true,
      int timeout = 3000}) async {
    Map<dynamic, dynamic> params = {
      'appKey': appKey,
      'appSecret': appSecret,
      'channel': channel,
      'timeout': timeout,
      'loggerEnable': loggerEnable
    };

    // 设置
    _channel.setMethodCallHandler(_setMethodCallHandler);
    return _channel.invokeMethod('init', params);
  }

  static Future<Map<dynamic, dynamic>> preLogin() async {
    return _channel.invokeMethod('preLogin');
  }

  static Future<Map<dynamic, dynamic>> getLoginToken(uiconfig) async {
    Map<String, dynamic> params = {'uiconfig': uiconfig};
    return _channel.invokeMethod('getLoginToken', params);
  }

  static Future<String> quitLoginPage() async {
    return _channel.invokeMethod('quitLoginPage');
  }

  static Future<String> hideLoginLoading() async {
    return _channel.invokeMethod('hideLoginLoading');
  }

  static Future<String> getVerifyId() async {
    return _channel.invokeMethod('getVerifyId');
  }

  static Future<String> getCurrentCarrierName() async {
    return _channel.invokeMethod('getCurrentCarrierName');
  }

  static addAuthRegistViewConfig(Map<dynamic, dynamic> params) async {
    _channel.invokeMethod('addAuthRegistViewConfig', params);
  }

  static addAuthRegisterXmlConfig(Map<dynamic, dynamic> params) async {
    _channel.invokeMethod('addAuthRegisterXmlConfig', params);
  }

  // 自定义控件的点击事件
  static customClickEventListener(CustomClickEventListener callback) {
    _eventHanders.customClickEvent = callback;
  }

  static sdkTokenEventListener(SDKTokenEventListener callback) {
    _eventHanders.sdkTokenEvent = callback;
  }

  static sdkClickEventListener(SDKClickEventListener callback) {
    _eventHanders.sdkClickEvent = callback;
  }

  static Future<void> _setMethodCallHandler(MethodCall call) async {
    switch (call.method) {
      case 'customClickEventListener':
        {
          if (_eventHanders.customClickEvent != null) {
            Map json = call.arguments.cast<dynamic, dynamic>();
            String widgetId = json['widgetId'];
            _eventHanders.customClickEvent(widgetId);
          }
        }
        break;
      case 'sdkTokenEventListener':
        {
          if (_eventHanders.sdkTokenEvent != null) {
            Map json = call.arguments.cast<dynamic, dynamic>();
            ListenerEventData res = ListenerEventData.fromJson({
              'code': json['code'],
              'msg': json['msg'],
              'data': json['token']
            });
            _eventHanders.sdkTokenEvent(res);
          }
        }
        break;
      case 'sdkClickEventListener':
        {
          if (_eventHanders.sdkClickEvent != null) {
            Map json = call.arguments.cast<dynamic, dynamic>();
            ListenerEventData res = ListenerEventData.fromJson(json);
            _eventHanders.sdkClickEvent(res);
          }
        }
        break;
      default:
        throw new UnsupportedError("无法识别的事件!");
    }
  }
}
